/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ACRestWebService;

import AjouConquerorBeans.BuildingSateInRBean;
import AjouConquerorBeans.GameRoomBean;
import AjouConquerorBeans.Nowuser;
import Classes.GameRoomClass;
import data.customLocation;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
 * REST Web Service
 *
 * @author ajou
 */
@Path("GameRoom")
public class GameRoomREST {

    BuildingSateInRBean buildingSateInRBean = lookupBuildingSateInRBeanBean();
    Nowuser nowuser = lookupNowuserBean();
    GameRoomBean gameRoomBean = lookupGameRoomBeanBean();
    ArrayList<GameRoomClass> Rooms = new ArrayList<GameRoomClass>();

    @POST
    @Path("addBFR")
    @Produces("text/plain")
    @Consumes("text/plain")
    public String addBuildingFRoom(String roomid) {
        int tp = Integer.parseInt(roomid);
        buildingSateInRBean.addbuildng(tp);
        String suc = "success";
        return suc;
    }

    @GET
    @Path("getRooms")
    @Produces("application/json")
    public ArrayList<GameRoomClass> getRooms() {
        Rooms = gameRoomBean.findallRooms();
        return Rooms;
    }

    @POST
    @Path("modifyroom")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String Modifyroom(@FormParam("uid") String id, @FormParam("roomid") String roomid) {
        String rooidst;
        rooidst = roomid;
        String whatisyourteam = gameRoomBean.ModifyRoom(Integer.parseInt(rooidst), id);
        System.out.println("whatisyourteam: " + whatisyourteam);
        return whatisyourteam;
    }

    @POST
    @Path("ready")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String ready(@FormParam("uid") String id, @FormParam("roomid") String roomid) {
        int a = Integer.parseInt(roomid);
        String rady = gameRoomBean.getready(a);
        return rady;
    }

    @POST
    @Path("forstart")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String forstart(@FormParam("uid") String id, @FormParam("roomid") String roomid) {
        int a = Integer.parseInt(roomid);
        gameRoomBean.setforReady(a);
        String w = "s";

        ArrayList<customLocation> users = new ArrayList<customLocation>();
        GameRoomClass g = gameRoomBean.getRoom(a);
        System.out.println(g.getTeam1_M1());
        users.add(new customLocation(gameRoomBean.getRoom(a).getTeam1_M1(), 0, 0));
        users.add(new customLocation(gameRoomBean.getRoom(a).getTeam1_M2(), 0, 0));
        users.add(new customLocation(gameRoomBean.getRoom(a).getTeam2_M1(), 0, 0));
        users.add(new customLocation(gameRoomBean.getRoom(a).getTeam2_M2(), 0, 0));

        nowuser.setRoom(a, users);
        return w;
    }

    @POST
    @Path("fornotstart")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String fornotstart(@FormParam("uid") String id, @FormParam("roomid") String roomid) {
        int tp = Integer.parseInt(roomid);
        Rooms.get(tp).setRady("notready");
        String w = "s";
        return w;
    }

    @POST
    @Path("AddRoom")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String addRoom(@FormParam("uid") String id, @FormParam("pass") String pass) {
        int roomid = gameRoomBean.CreateRoom(pass, id);
        String a = Integer.toString(roomid);
        return a;
    }

    @POST
    @Path("teamchange")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String changeteam(@FormParam("uid") String id, @FormParam("roomid") String roomid) {
        String result = gameRoomBean.changeteam(roomid, id);

        return result;
    }

    @POST
    @Path("removeuser")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String removeuser(@FormParam("uid") String id, @FormParam("roomid") String roomid) {
        String check = id + " is removed from room";
        String rooidst;
        rooidst = roomid;
        gameRoomBean.removeuser(Integer.parseInt(rooidst), id);
        System.out.println(check);
        return check;
    }

    private GameRoomBean lookupGameRoomBeanBean() {
        try {
            Context c = new InitialContext();
            return (GameRoomBean) c.lookup("java:global/AjouConquerorServer/AjouConquerorServer-ejb/GameRoomBean!AjouConquerorBeans.GameRoomBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private Nowuser lookupNowuserBean() {
        try {
            Context c = new InitialContext();
            return (Nowuser) c.lookup("java:global/AjouConquerorServer/AjouConquerorServer-ejb/Nowuser!AjouConquerorBeans.Nowuser");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private BuildingSateInRBean lookupBuildingSateInRBeanBean() {
        try {
            Context c = new InitialContext();
            return (BuildingSateInRBean) c.lookup("java:global/AjouConquerorServer/AjouConquerorServer-ejb/BuildingSateInRBean!AjouConquerorBeans.BuildingSateInRBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
